package site.xuqing.autohelperengine.local.overlay;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;

import site.xuqing.autohelperengine.AutoHelper;
import site.xuqing.autohelperengine.R;
import site.xuqing.autohelperengine.StepManager;
import site.xuqing.autohelperengine.local.bean.StepLocalBean;
import site.xuqing.autohelperengine.local.bean.StepLocalSaveBean;
import site.xuqing.autohelperengine.local.bean.StepLocalAddType;
import site.xuqing.autohelperengine.util.LogUtil;

/**
 * @author xuqing
 */
public class MenuView extends LinearLayout implements View.OnClickListener {
    Button startStep;
    Button executeStepStart;
    Button executeStepEnd;
    Button returnApp;
    Button saveStep;
    LinearLayout taskLayout;
    Button loopStart;
    Button loopEnd;
    Button nextStep;
    Button complete;

    private GestureDetector mGestureDetector;

    private StepLocalSaveBean stepLocalSaveBean;
    private StepLocalBean stepLocalBean;

    public MenuView(Context context) {
        this(context, null);
    }

    public MenuView(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.float_menu, this);
        mGestureDetector = new GestureDetector(context, new MyOnGestureListener());
        initView();
    }

    @SuppressLint("ClickableViewAccessibility")
    private void initView() {
        startStep=findViewById(R.id.start_step);
        executeStepStart=findViewById(R.id.execute_step_start);
        executeStepEnd=findViewById(R.id.execute_step_end);
        returnApp=findViewById(R.id.return_app);
        saveStep=findViewById(R.id.save_step);
        taskLayout=findViewById(R.id.task_layout);
        loopStart=findViewById(R.id.loop_start);
        loopEnd=findViewById(R.id.loop_end);
        nextStep=findViewById(R.id.next_step);
        complete=findViewById(R.id.complete);
        startStep.setOnClickListener(this);
        executeStepStart.setOnClickListener(this);
        executeStepEnd.setOnClickListener(this);
        returnApp.setOnClickListener(this);
        loopStart.setOnClickListener(this);
        loopEnd.setOnClickListener(this);
        saveStep.setOnClickListener(this);
        nextStep.setOnClickListener(this);
        complete.setOnClickListener(this);
        executeStepEnd.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                mGestureDetector.onTouchEvent(event);
                return true;
            }
        });
    }

    public void setStepLocalBean(StepLocalBean stepLocalBean) {
        returnApp.setVisibility(GONE);
        if (stepLocalBean == null) {
            startStep.setVisibility(View.VISIBLE);
            executeStepStart.setVisibility(View.GONE);
            executeStepEnd.setVisibility(View.GONE);
            saveStep.setVisibility(View.GONE);
            taskLayout.setVisibility(ViewGroup.GONE);
        } else {
            startStep.setVisibility(View.GONE);
            executeStepStart.setVisibility(View.GONE);
            executeStepEnd.setVisibility(View.GONE);
            saveStep.setVisibility(View.GONE);
            taskLayout.setVisibility(ViewGroup.VISIBLE);
            switch (StepManager.currentStepLoopState) {
                case 0:
                    loopStart.setVisibility(View.VISIBLE);
                    loopEnd.setVisibility(View.GONE);
                    break;
                case 1:
                    loopStart.setVisibility(View.GONE);
                    loopEnd.setVisibility(View.VISIBLE);
                    break;
                case 2:
                    loopStart.setVisibility(View.GONE);
                    loopEnd.setVisibility(View.GONE);
                    break;
                default:
                    break;
            }
        }
        this.stepLocalSaveBean=null;
        this.stepLocalBean = stepLocalBean;
    }

    public void setStepBeans(StepLocalSaveBean stepLocalSaveBean) {
        startStep.setVisibility(View.GONE);
        executeStepStart.setVisibility(View.VISIBLE);
        executeStepEnd.setVisibility(View.GONE);
        returnApp.setVisibility(VISIBLE);
        saveStep.setVisibility(View.GONE);
        taskLayout.setVisibility(ViewGroup.GONE);
        this.stepLocalSaveBean=stepLocalSaveBean;
        StepManager.setLocalSteps(stepLocalSaveBean);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.start_step) {
            StepManager.currentStepLoopState = 0;
            FloatViewManager.getInstance().showCoverView();
            FloatViewManager.getInstance().hideMessageView();
            FloatViewManager.getInstance().hideMenuView();
            StepManager.reset();
        } else if (id == R.id.execute_step_start) {
            StepManager.executeStart();
            startStep.setVisibility(View.GONE);
            executeStepStart.setVisibility(View.GONE);
            executeStepEnd.setVisibility(View.VISIBLE);
            returnApp.setVisibility(GONE);
            saveStep.setVisibility(View.GONE);
            taskLayout.setVisibility(ViewGroup.GONE);
        } else if (id == R.id.execute_step_end) {
            StepManager.executeStop();
            if (stepLocalSaveBean!=null&&stepLocalSaveBean.getStepLocalType()== StepLocalAddType.INSET){
                startStep.setVisibility(View.GONE);
                executeStepStart.setVisibility(View.VISIBLE);
                executeStepEnd.setVisibility(View.GONE);
                returnApp.setVisibility(VISIBLE);
                saveStep.setVisibility(View.GONE);
                taskLayout.setVisibility(ViewGroup.GONE);
            }else {
                startStep.setVisibility(View.VISIBLE);
                executeStepStart.setVisibility(View.GONE);
                executeStepEnd.setVisibility(View.GONE);
                returnApp.setVisibility(GONE);
                saveStep.setVisibility(View.VISIBLE);
                taskLayout.setVisibility(ViewGroup.GONE);
            }
        } else if(id == R.id.return_app){
            AutoHelper.getInstance().startApp(MenuView.this.getContext(),MenuView.this.getContext().getPackageName());
            FloatViewManager.getInstance().hideMenuView();
        }else if (id == R.id.loop_start) {
            StepManager.currentStepLoopState = 1;
            loopStart.setVisibility(View.GONE);
            loopEnd.setVisibility(View.VISIBLE);
            stepLocalBean.setLoopStart(true);
        } else if (id == R.id.loop_end) {
            StepManager.currentStepLoopState = 2;
            loopStart.setVisibility(View.GONE);
            loopEnd.setVisibility(View.GONE);
            stepLocalBean.setLoopEnd(true);
        } else if (id == R.id.next_step) {
            StepManager.setLocalStep(stepLocalBean);
            FloatViewManager.getInstance().hideMenuView();
            FloatViewManager.getInstance().hideMessageView();
            FloatViewManager.getInstance().showCoverView();
        } else if (id == R.id.complete) {
            StepManager.setLocalStep(stepLocalBean);
            FloatViewManager.getInstance().hideCoverView();
            FloatViewManager.getInstance().showMessageView("步骤已完成，您可以点击执行步骤，来执行自动化程序");
            startStep.setVisibility(View.VISIBLE);
            executeStepStart.setVisibility(View.VISIBLE);
            executeStepEnd.setVisibility(View.GONE);
            returnApp.setVisibility(GONE);
            saveStep.setVisibility(View.VISIBLE);
            taskLayout.setVisibility(ViewGroup.GONE);
        } else if (id == R.id.save_step) {
            FloatViewManager.getInstance().showInputView();
        }
    }

    private String getActionName(int action) {
        String name = "";
        switch (action) {
            case MotionEvent.ACTION_DOWN: {
                name = "ACTION_DOWN";
                break;
            }
            case MotionEvent.ACTION_MOVE: {
                name = "ACTION_MOVE";
                break;
            }
            case MotionEvent.ACTION_UP: {
                name = "ACTION_UP";
                break;
            }
            default:
                break;
        }
        return name;
    }

    class MyOnGestureListener extends GestureDetector.SimpleOnGestureListener {
        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            LogUtil.i(getClass().getName(), "onSingleTapUp-----" + getActionName(e.getAction()));
            return false;
        }
        @Override
        public void onLongPress(MotionEvent e) {
            LogUtil.i(getClass().getName(), "onLongPress-----" + getActionName(e.getAction()));
        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            LogUtil.i(getClass().getName(),
                    "onScroll-----" + getActionName(e2.getAction()) + ",(" + e1.getX() + "," + e1.getY() + ") ,("
                            + e2.getX() + "," + e2.getY() + ")");
            return false;
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            LogUtil.i(getClass().getName(),
                    "onFling-----" + getActionName(e2.getAction()) + ",(" + e1.getX() + "," + e1.getY() + ") ,("
                            + e2.getX() + "," + e2.getY() + ")");
            return false;
        }

        @Override
        public void onShowPress(MotionEvent e) {
            LogUtil.i(getClass().getName(), "onShowPress-----" + getActionName(e.getAction()));
        }

        @Override
        public boolean onDown(MotionEvent e) {
            LogUtil.i(getClass().getName(), "onDown-----" + getActionName(e.getAction()));

            StepManager.executeStop();
            if (stepLocalSaveBean!=null&&stepLocalSaveBean.getStepLocalType()== StepLocalAddType.INSET){
                startStep.setVisibility(View.GONE);
                executeStepStart.setVisibility(View.VISIBLE);
                executeStepEnd.setVisibility(View.GONE);
                returnApp.setVisibility(VISIBLE);
                saveStep.setVisibility(View.GONE);
                taskLayout.setVisibility(ViewGroup.GONE);
            }else {
                startStep.setVisibility(View.VISIBLE);
                executeStepStart.setVisibility(View.GONE);
                executeStepEnd.setVisibility(View.GONE);
                returnApp.setVisibility(GONE);
                saveStep.setVisibility(View.VISIBLE);
                taskLayout.setVisibility(ViewGroup.GONE);
            }
            return false;
        }

        @Override
        public boolean onDoubleTap(MotionEvent e) {
            LogUtil.i(getClass().getName(), "onDoubleTap-----" + getActionName(e.getAction()));
            return false;
        }

        @Override
        public boolean onDoubleTapEvent(MotionEvent e) {
            LogUtil.i(getClass().getName(), "onDoubleTapEvent-----" + getActionName(e.getAction()));
            return false;
        }

        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            LogUtil.i(getClass().getName(), "onSingleTapConfirmed-----" + getActionName(e.getAction()));
            return false;
        }
    }
}
